#ifndef Dan_UVOPERATOR
#define Dan_UVOPERATOR
#include "Const.cginc"

//-------------------- uv 变换 -------------------

//uv 重映射
float Remap (float oldMin,float oldMax,float newMin, float newMax,float val)
{ 
  float percent=(val-oldMin)/(oldMax-oldMin);
  return percent*(newMax-newMin)+newMin;
}

// uv 映射成矩形
float2 Remap2Rect(float2 uv,float4 rect)
{
  uv.x = lerp(rect.x,rect.x+rect.z, uv.x);
  uv.y = lerp(rect.y,rect.y+rect.w, uv.y);
  return uv;
}
//缩放UV
float2 ScaleUV(float2 uv ,float scaleVal){
  float2 scaleUV = (scaleVal+2*uv-1)/(2*scaleVal);
  return scaleUV;
}
//2d旋转
float2 Rotate2D(float2 uv, float2 center ,float rotVal){
  float2x2 m = float2x2( cos(rotVal), -sin(rotVal), sin(rotVal), cos(rotVal));
  float2 rotUV = mul(uv-center,m)+center;
  return rotUV;
}
//uv 按弧度旋转
float2 Rotate2D_Rad(float2 uv,float2 center,float rad)
{
	uv -= center;
	float2x2 m = float2x2(cos(rad),sin(rad),-sin(rad),cos(rad));
	uv = mul(m,uv)+center;
	return uv;
}

//uv 按角度旋转
float2 Rotate2D_Deg(float2 uv,float2 center, float deg)
{
    deg *= _Deg2Rad;//PI/180
    uv= uv-center;
    float s = sin(deg);
    float c = cos(deg);
    float2x2 ma = float2x2(c, -s, s, c);
    uv = mul(uv, ma)+center;
    return uv;
}

//uv水平波浪线
float UVWaveLine(float2 uv,float range, float frequency , float smoothWave,float speed , float height){
  float waveFlow = sin(uv.x*_PI2*frequency+_Time.y*speed);
  float vertical =lerp(0,waveFlow,smoothWave);               
  float horizontal =((height*2-1)*range+(uv.y-0.5)*range);
  float wave = step((horizontal-vertical),0.5);
  return wave;
}

//uv波浪
float2  UV_Wave (float2 uv,float flowSpeed,float waveCount,float amplitude)          
{                    
   uv.x +=_Time.y * flowSpeed;                   
   uv.y+=sin( uv.x  * _PI2 *waveCount  ) * amplitude;                    
   return uv;           
}

//uv 扭曲01
float2 DistortUV01(float2 p,float speed)
{
   speed*=_Time.y;
   p=float2(p.x+sin(speed),p.y+speed);
   return p;       
}
//uv 扭曲02
float2 DistortUV02(float2 p, float waveX, float waveY, float distanceX, float distanceY, float speed)
{
    speed *=_Time.y;
    p.x= p.x+sin(speed+p.y*waveX )*distanceX;
    p.y= p.y+cos(speed+p.x*waveY)*distanceY;
    return p;
}
// uv 收缩
float2 Pinch( float2 uv , float size )
{
    float2 m = float2(0.5, 0.5);
    float2 d = uv - m;
    float r = sqrt(dot(d, d));
    float amount = (_PI2 / (2.0 * sqrt(dot(m, m)))) * (-size+0.001);
    float bind = 0.5;
    uv = m + normalize(d) * atan(r * -amount * 16.0) * bind / atan(-amount * bind * 16.0);
    return uv;
}

//uv 漩涡
float2 Twirl( float2 uv , float2 center , float value , float radius )
{
    value = value * _Deg2Rad;
    uv -= center;
    float2 distortedOffset = Rotate2D_Rad(uv,0.5,value);
    float2 tmp = uv / radius;
    float t = min (1, length(tmp));
    uv = lerp (distortedOffset, uv, t);
    uv += center;
    return uv;
}

//uv 螺旋
float2 Spiral(float2 uv, float2 center,float strength)
{
    uv-=center;
    float angle = 50 * length(uv)*strength;
    float x = cos(angle) * uv.x - sin(angle) * uv.y;
    float y = sin(angle) * uv.x + cos(angle) * uv.y;
    return float2( x ,y);
}

// uv 鱼眼01
float2 FishEye01( float2 uv ,float2 m, float size )
{
   float2 d = uv - m;
   float r = sqrt(dot(d, d));
   float bind = sqrt(dot(m, m));
   float amount = (_PI2 / (2.0 * bind)) * (size*0.5+0.0001);
   uv = m + normalize(d) * tan(r * amount) * bind/ tan(bind * amount);
   return uv;
}

// uv 鱼眼02
float2 FishEye02(float2 uv, float2 center,float2 offset,float strength)
{
   float2 delta = uv - center;
   float delta2 = dot(delta.xy, delta.xy);
   float delta4 = delta2 * delta2;
   float2 delta_offset = delta4 *strength;
   float2 outUV = uv + delta * delta_offset + offset;
   return outUV;    
}

//极坐标
float DrawPolar(float2 uv){
    float polar = (atan2(uv.x,uv.y)/_PI2)+0.5;
    return polar;
}
//极坐标模拟CD冷却
float Polar (float2 uv , float progress){
    uv-=0.5;
    float a = atan2(uv.x,uv.y);
    a = Remap(-_PI,_PI,0.1,1,a);
    return smoothstep(0,0.001,a-progress);
}


//极坐标扫描



//------------uv 遮罩-----------------

//线条Mask
float UVMaskLine (float uvdir, float amount, float width){float d = abs((frac(uvdir*amount)-0.5)*2);return floor(d+width);}
                
//波点Mask  可改变点的形状              
float UVMaskPoint(float2 uv, float amount ,float smooth){ uv = abs((frac(uv*amount)-0.5)*2);uv =uv.x + uv.y - 2.0 * uv.x * uv.y;   
  return floor(uv+smooth);}

//方形棋盘格Mask
float UVMaskCheck01(float2 uv, float amount){ uv = floor(uv*amount); float t = fmod (uv.x+1,2); uv =fmod (uv,2);return lerp(t,uv.x,uv.y); }

//棱形棋盘格Mask
float UVMaskCheck02(float2 uv, float amount , float width){ uv = abs((frac(uv*amount)-0.5)*2);uv.x+=1-uv.y;return floor(uv-width); }

//网状Mask
float UVMaskNet (float2 uv, float amount, float size){ uv =abs((frac(uv*amount)-0.5)*2);return 1-floor(abs(uv.x -uv.y)+size);}

//格子Mask
float UVMaskGrid (float2 uv, float amount ,float size){ uv = abs((frac(uv*amount)-0.5)*2); uv =1-max(uv.x,uv.y);return floor(uv+size);}

//圆点Mask
float UVMaskSpots(float2 uv, float amount, float size){ uv= (frac(uv*amount)-0.5)*2; return floor(1-length(uv)+size);}

//蜂窝Mask
float UVMaskHoneycomb (float2 uv , float amount, float width){
 uv*=amount;
 uv.x*=2/sqrt(3);
 float x =floor(uv.x);
 x =fmod(x,2)*0.5;   
 float c1 =abs(frac(uv.x)-0.5)*1.5;
 float c2 =abs(frac(x+uv.y)-0.5);
 float c3 =max(c1+c2, c2*2)-1;  
 return step(0.1,abs(c3));
}
//环形蛛网Mask
float UVMaskCircleGrid(float2 uv, float2 size, float width){
  float2 detla= uv-0.5;
  float polar = atan2(detla.x ,detla.y) / 6.28318548202515;
  float circle = length( detla)*2;
  float p1 =step( frac( ( polar *32*size.y ) ) , width );
  float p2 =step( frac( ( circle*8*size.x  ) ) , width);
  return p1+p2;
}    



//---------------- uv 图形 -----------------


//uv画硬线
float DrawHardLine (float2 uv ,float size ){return step(size,uv.y)*step(size,1-uv.y);}
//uv画软线
float DrawSoftLine(float2 uv, float pct, float val){return smoothstep(pct-val,pct,uv.y)-smoothstep(pct,pct+val,uv.y);}

//uv画圆01
float DrawCircle01(float2 uv,float radius,float blur)
 {
   uv-=0.5;
   float circle =length(uv)*2;
   circle =1-smoothstep(radius-radius*blur,radius+radius*blur,circle); 
   return  circle;
 }  
//uv画圆02
float DrawCircle02 (float2 uv ,float2 offset, float2 size , float2 SSMinMax)
{
  uv-=0.5;
  uv-=offset;
  uv/=size;
  float circle = 1-length(uv)*2;
  return smoothstep(SSMinMax.x,SSMinMax.y,circle);
}

//uv画矩形01
float DrawRect01 (float2 uv, float2 size,float blur)
{
   float2 d = abs(uv * 2 - 1) - size;
   d = 1 - d / smoothstep(-blur,1,fwidth(d));
   d = min(d.x, d.y);
   return saturate(d) ;
}
//uv画矩形02
float DrawRect02(float2 uv , float2 size, float blur,float bevel )
{
  size =0.5-size*0.5;
  uv.x+=(uv.y*size-size)*bevel;
  float2 shape =smoothstep(size,size+blur*0.5,uv);
  shape*=smoothstep(size,size+blur*0.5,1-uv);
  return shape.x*shape.y;
}
//uv画圆角矩形
float DrawRoundedRect(float2 uv , float2 size , float radius ,float blur)
{
 radius= max(min(min(abs(radius * 2), abs(size.x)), abs(size.y)), 1e-5);
 uv = abs(uv * 2 - 1) - size + radius;
 float d = length(max(0, uv)) / radius;
 return saturate((1 - d) / smoothstep(-blur,1,fwidth(d)));
}

//uv画弧边矩形
float  DrawCurvedRect (float2 uv , float size , float blur){
  uv =(1.0 - uv)*uv*size;
  float shape = smoothstep( 0, blur, saturate(uv.x*uv.y)); 
  return shape;
}

//uv画网状格子
float DrawNet (float2 uv , float2 netUV , float width){
  netUV = frac(netUV*uv)*2-1;
  float net = saturate(max(abs(netUV.x),abs(netUV.y)));
  return step(1.0 - width,net);
}

//uv画多边形01
float DrawPolygon01 (float2 uv, float radius, float sides)
{
  uv=uv*2-1;
  uv/=radius;
  float angle = atan2(uv.x,uv.y)+_PI;
  sides= _PI2/sides;
  float shape=cos(floor(0.5+angle/sides)*sides-angle)*length(uv);
  return 1-shape;
}

//uv画多边形02
float DrawPolygon02(float2 uv, float radius, float sides, float blur)
{
  uv=uv*2-1;
  float angle = atan2(uv.x,uv.y)+_PI;
  sides= _PI2/sides;
  float shape=cos(floor(0.5+angle/sides)*sides-angle)*length(uv);
  return 1-smoothstep(radius-blur,radius,shape);
}

//uv 画箭头
float DrawArrow(float2 uv, float width, float amount){
  float size =width*0.5+0.5;
  float val =frac(saturate(abs(uv.y-0.5)+frac(uv.x*amount))); 
  return step(size,val);    
}

//uv 画环形 shape填入形状
float DrawRing(float shape , float radius , float radiusinside){
  float val =saturate(abs(step(radius,shape)-step(min(radius,radiusinside),shape)));
  return val;
}
//uv 画心 位置 0.5,0.35 大小 2.1，2.7
float DrawHeart(float2 uv ,float2 offset,float2 size){
  float2 tempUV = (uv-offset)*size;
  float shapeU = tempUV.x;
  float shapeArc = tempUV.y-sqrt(atan(saturate(abs(shapeU)+0.125)));
  float heart = saturate(shapeU*shapeU+shapeArc*shapeArc);
  return heart;
}
//uv根据形状画外描边 返回形状大小和边缘线
void DrawOutLine(out float shapeSize, out float outline,float radius, float outlineVal, float shape){
  shapeSize = step(shape,radius);
  outline = step((radius-outlineVal),shape);        
}

//画虚线 带转动效果
float DrawDash(float polarShape, float circleShape,  float amount,float speed, float length){
  float dashFlow = frac(polarShape*amount+_Time.y*speed);
  dashFlow =step(length,dashFlow);
  float dashMask =saturate(abs(floor(circleShape)-step(0.95,circleShape)));
  return dashFlow*dashMask;
}




#endif